package DataAccess
{
	import Model.ClimateZone;
	
	import mx.collections.ArrayCollection;
	import mx.core.FlexGlobals;
	import mx.rpc.CallResponder;
	import mx.rpc.events.ResultEvent;
	
	import services.climatezonesservice.ClimatezonesService;
	
	import valueObjects.ClimateZones;

	public class DataAccess
	{
		private var climateZoneCallResponder:CallResponder;
		private var climateZoneService:ClimatezonesService;
		
		
		public function DataAccess()
		{
			this.climateZoneCallResponder = new CallResponder();
			this.climateZoneService = new ClimatezonesService();
		}
		
		public function getClimateZoneByCountry(id:Number, language:String):void
		{
			this.climateZoneCallResponder = new CallResponder();
			
			if(language == "en_US")
			{
				language = "en_UK";
			}
			
			climateZoneCallResponder.token = climateZoneService.getClimateByCountryID(id , language);
			climateZoneCallResponder.addEventListener(ResultEvent.RESULT, setClimateZones);
		}
		
		protected function setClimateZones(event:ResultEvent):void
		{
			var arr:ArrayCollection = climateZoneCallResponder.lastResult;
			
			var climateZone:ClimateZone;
			
			for each(var zone:ClimateZones in arr)
			{
				climateZone = new ClimateZone();
				
				climateZone.countryId = zone.country_id;
				climateZone.label = zone.name;
				climateZone.ht = zone.ht;
				climateZone.gt = zone.gt;
				climateZone.globalEast = zone.global_east;
				climateZone.globalNorth = zone.global_north;
				climateZone.globalSouth = zone.global_south;
				climateZone.globalWest = zone.global_west;
				climateZone.globalHorizontal = zone.global_horizontal;

				FlexGlobals.topLevelApplication.screenCollection[0].climateZonesDropDownDataProvider.addItem(climateZone);
			}
		}
	}
}